﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
using System.Data;
using System.Linq;

public partial class chapter : BasePage
{
    protected int chapterId
    {
        get { return ViewState["Chapter_Id"] == null ? 0 : (int)ViewState["Chapter_Id"]; }
        set { ViewState["Chapter_Id"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Context.Items["showNav"] = false;
        if (!IsPostBack)
        {
            int cid = 0;
            int.TryParse(Request["cid"], out cid);
            chapterId = cid;

            this.InitChapterData(cid);
        }
    }
    private void InitChapterData(int cid)
    {
        DbHelper db = new DbHelper();
        string sql = "SELECT * FROM Chapter (NOLOCK) WHERE cid = " + cid;
        DataTable tbChapter = db.ExecQuery(sql);
        if (tbChapter != null && tbChapter.Rows.Count > 0)
        {
            foreach (DataColumn col in tbChapter.Columns) Entity[col.ColumnName] = tbChapter.Rows[0][col];

            sql = "SELECT a.*,b.fullPath FROM Video a (NOLOCK) LEFT JOIN  Attachment b (NOLOCK) ON a.aid = b.aid WHERE a.cid = " + cid;
            DataTable bt = db.ExecQueryEx(sql);
            object[] videos = new object[bt.Rows.Count];
            for (int i = 0; i < bt.Rows.Count; i++)
            {
                videos[i] = new { vid = bt.Rows[i]["vid"], href = base.GetSitePath(bt.Rows[i]["fullPath"].ToStringEx()), thumbnail = "/content/images/video-128.png", title = bt.Rows[i]["title"] };
            }
            this.lstVideo.DataSource = videos;
            this.lstVideo.DataBind();
            //0 单选 1 多选 2 填空 3 简答
            sql = "SELECT *, (CASE type WHEN 0 THEN '单选' WHEN 1 THEN '多选' WHEN 2 THEN '填空' WHEN 3 THEN '简答' ELSE '' END) typeName "
                 + "FROM Question A (NOLOCK) WHERE qid IN (SELECT AA.qid FROM PracticeQuestion AA WHERE AA.pid = {0})";
            DataTable tbQuestions = db.ExecQueryEx(sql, tbChapter.Rows[0].Value<decimal>("pid"));

            sql = "SELECT * FROM QuestionOption (NOLOCK) WHERE pid IN (SELECT A.qid FROM Question A (NOLOCK) WHERE qid IN (SELECT AA.qid FROM PracticeQuestion AA WHERE AA.pid = {0}))";
            DataTable tbOptions = db.ExecQueryEx(sql, tbChapter.Rows[0].Value<decimal>("pid"));


            listQuestions.DataSource = tbQuestions.AsEnumerable().Select(r => new {
                qid = r.Value<int>("qid"),
                type = r.Value<int>("type"),
                title = r.Value("title"),
                content = r.Value("content"),
                score = r.Value<decimal>("score"),
                options = this.GetOptions(tbOptions, r.Value<int>("qid")),
                answers = string.Join(",", tbOptions.Select("answer = right AND pid = " + r.Value<decimal>("qid")).Select(row => row.Value("answer")))
            }).ToArray();
            listQuestions.DataBind();
        }
    }

    private string GetOptions(DataTable tbOptions, int qid)
    {
        return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(tbOptions.Select("pid = " + qid).Select(r => new {
            oid = r.Value<int>("oid"),
            qid = r.Value<int>("pid"),
            answer = r.Value("answer"),
            right = r.Value("right"),
            score = r.Value<decimal>("score")
        }).ToArray());
    }
}